package com.stars.easyms.logger.support.logback.compatibility;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.stars.easyms.base.util.ClassExistsUtil;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>className: CowArrayListCompatibility</p>
 * <p>description: COWArrayList兼容类：在1.1.10之下的版本无此类</p>
 *
 * @author guoguifang
 * @date 2019-12-25 13:52
 * @since 1.5.0
 */
public class CowArrayListCompatibility {

    private static final boolean EXIST_COW_ARRAY_LIST_CLASS = ClassExistsUtil.isExist("ch.qos.logback.core.util.COWArrayList");

    public static void addIfAbsent(List<Appender<ILoggingEvent>> appenderList, Appender<ILoggingEvent> newAppender) {
        if (EXIST_COW_ARRAY_LIST_CLASS) {
            CowArrayListHandler.addIfAbsent(appenderList, newAppender);
        } else {
            ((CopyOnWriteArrayList<Appender<ILoggingEvent>>) appenderList).addIfAbsent(newAppender);
        }
    }

    @SuppressWarnings("unchecked")
    public static Appender<ILoggingEvent>[] asTypedArray(List<Appender<ILoggingEvent>> appenderList) {
        if (EXIST_COW_ARRAY_LIST_CLASS) {
            return CowArrayListHandler.asTypedArray(appenderList);
        } else {
            return appenderList.toArray(new Appender[0]);
        }
    }

}